//
//  UserAuthApi.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/23.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import Moya

enum UserAuthApi {
    case userRegister(username: String, password: String)
    case getToken(username: String, password: String)
    case applyForRegisterEmail(email: String)
    case totpVerification(email: String, totp: String)
    case getTotp()
    case getForgetPWTotp(email: String)
    case changePW(newPW: String)
}

extension UserAuthApi: TargetType {
    
    var baseURL: URL {
        return URL(string: serverBasePath)!
    }
    
    var path: String {
        switch self {
        case .userRegister:
            return "/users"
        case .getToken:
            return "/auth/token"
        case .applyForRegisterEmail:
            return "/auth/applyForRegistrationEmail"
        case .getForgetPWTotp:
            return "/auth/totp"
        case .totpVerification:
            return "/auth/totp"
        case .getTotp:
            return "/totp"
        case .changePW:
            return "/auth/password"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .userRegister, .getToken, .totpVerification:
            return .post
        case .applyForRegisterEmail, .getTotp, .getForgetPWTotp:
            return .get
        case .changePW:
            return .put
        }
    }
    
    var parameters: [String: Any]? {
        switch self {
        case .applyForRegisterEmail(let email):
            return ["email": email]
        case .getForgetPWTotp(let email):
            return ["email": email]
        case .getToken(let username, let password):
            return ["username": username, "password": password]
        case .totpVerification(let email, let totp):
            return ["email": email, "totp": totp]
        case .userRegister(let username, let password):
            return ["username": username, "password": password]
        case .getTotp:
            return nil
        case .changePW(let newPW):
            return ["value": newPW]
        }
    }
    
    var task: Task {
        return .request
    }
    
    var sampleData: Data {
        return "{\"data\":{\"msg\":\"good\"},\"meta\":{\"status\":200,\"msg\":\"OK\"}}".data(using: String.Encoding.utf8)!
    }
    
    var parameterEncoding: ParameterEncoding {
        switch self {
        case .getToken, .totpVerification, .userRegister, .changePW:
            return JSONEncoding.default
        default:
            return URLEncoding.default
        }
    }
}
